package org.my.stock.controller;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.my.stock.util.MyStockDawnSqlUtil;
import org.my.stock.util.MyStockUtil;
import org.my.stock.util.MyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyVetoException;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.List;

@Controller
public class MyStockController {

    @RequestMapping(value = "/stock_data_test")
    public @ResponseBody
    String stock_data_test(ModelMap model, HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value = "code", required = false) String code) throws UnsupportedEncodingException {


        return "吴大富：" + code;
    }

    public boolean is_insert(String table_name, String name, Object st_timestamp) throws PropertyVetoException, SQLException, ClassNotFoundException {
        Connection conn = MyUtil.getConnection();
        boolean flag = false;
        try {
            PreparedStatement statement = conn.prepareStatement("select m.id as id from " + table_name + " as m where m.name = ? and m.st_timestamp = ?");
            statement.setString(1, name);
            statement.setDate(2, new Date((int)st_timestamp));
            ResultSet rs = statement.executeQuery();
            while (rs.next())
            {
                int id = rs.getInt("id");
                if (id > 0)
                {
                    flag = true;
                    break;
                }
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally {
            conn.close();
        }

        return flag;
    }

    public void insert_data(String table_name, String name, List<List<Object>> lst, String symbol) throws SQLException, ClassNotFoundException, PropertyVetoException {
        Connection conn = MyUtil.getConnection();

        for (List<Object> r : lst) {
            if (is_insert(table_name, name, r.get(0))) {
                PreparedStatement statement = conn.prepareStatement("insert into " + table_name + " (st_timestamp, volume, open, high, low, close, chg, percent, turnoverrate, amount, volume_post, amount_post, pe, pb, ps, pcf, market_capital, balance, hold_volume_cn, hold_ratio_cn, net_volume_cn, hold_volume_hk, hold_ratio_hk, net_volume_hk, symbol, name) " +
                        "values (?, ?, ?, ?, ?, " +
                        "?, ?, ?, ?, ?," +
                        "?, ?, ?, ?, ?," +
                        "?, ?, ?, ?, ?," +
                        "?, ?, ?, ?, ?, ?)");
                statement.setDate(1, new Date((int) r.get(0)));
                statement.setDouble(2, r.get(1) != null ? (Double) r.get(1) : null);
                statement.setDouble(3, r.get(2) != null ? (Double) r.get(2) : null);
                statement.setDouble(4, r.get(3) != null ? (Double) r.get(3) : null);
                statement.setDouble(5, r.get(4) != null ? (Double) r.get(4) : null);
                statement.setDouble(6, r.get(5) != null ? (Double) r.get(5) : null);
                statement.setDouble(7, r.get(6) != null ? (Double) r.get(6) : null);
                statement.setDouble(8, r.get(7) != null ? (Double) r.get(7) : null);
                statement.setDouble(9, r.get(8) != null ? (Double) r.get(8) : null);
                statement.setDouble(10, r.get(9) != null ? (Double) r.get(9) : null);
                statement.setDouble(11, r.get(10) != null ? (Double) r.get(10) : null);

                statement.setDouble(12, r.get(11) != null ? (Double) r.get(11) : null);
                statement.setDouble(13, r.get(12) != null ? (Double) r.get(12) : null);
                statement.setDouble(14, r.get(13) != null ? (Double) r.get(13) : null);
                statement.setDouble(15, r.get(14) != null ? (Double) r.get(14) : null);
                statement.setDouble(16, r.get(15) != null ? (Double) r.get(15) : null);
                statement.setDouble(17, r.get(16) != null ? (Double) r.get(16) : null);
                statement.setDouble(18, r.get(17) != null ? (Double) r.get(17) : null);
                statement.setDouble(19, r.get(18) != null ? (Double) r.get(18) : null);
                statement.setDouble(20, r.get(19) != null ? (Double) r.get(19) : null);
                statement.setDouble(21, r.get(20) != null ? (Double) r.get(20) : null);
                statement.setDouble(22, r.get(21) != null ? (Double) r.get(21) : null);
                statement.setDouble(23, r.get(22) != null ? (Double) r.get(22) : null);
                statement.setDouble(24, r.get(23) != null ? (Double) r.get(23) : null);
                statement.setString(25, symbol);
                statement.setString(26, name);

                statement.executeUpdate();
            }
            else
            {
                break;
            }
        }
        conn.close();
    }

    @CrossOrigin(origins = "https://xueqiu.com", maxAge = 3600)
    @RequestMapping(value = "/run_sql_data")
    public @ResponseBody
    String run_sql_data(ModelMap model, HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "symbol", required = false) String symbol,
                        @RequestParam(value = "code", required = false) String code,
                        @RequestParam(value = "name", required = false) String name,
                        @RequestParam(value = "period", required = false) String period) throws SQLException, ClassNotFoundException, PropertyVetoException {

        String jsonpCallback = request.getParameter("callback");
        if (code != null) {
            List<List<Object>> lst = MyUtil.getGson().fromJson(code, new TypeToken<List<List<Object>>>() {}.getType());
            insert_data("stock_date", name, lst, symbol);
            System.out.println(lst);
            return jsonpCallback + "({data:'true'})";
        }
        return jsonpCallback + "({data:'false'})";
    }

    @CrossOrigin(origins = "https://xueqiu.com", maxAge = 3600)
    @RequestMapping(value = "/stock_data")
    public @ResponseBody
    String stock_data(ModelMap model, HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "symbol", required = false) String symbol,
                        @RequestParam(value = "name", required = false) String name,
                        @RequestParam(value = "my_type", required = false) String my_type) throws SQLException, ClassNotFoundException, PropertyVetoException {

        String jsonpCallback = request.getParameter("callback");
        if (!Strings.isNullOrEmpty(symbol)) {
            System.out.println("symbol: " + symbol + " name: " + name + " my_type: " + my_type);
            MyStockDawnSqlUtil.has_stock(symbol, name, my_type);
            return jsonpCallback + "({data:'true', symbol: '"+ symbol +"', name: '"+ name +"', my_type: '"+ my_type +"'})";
        }
        return jsonpCallback + "({data:'false'})";
    }
}






































